﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using caedisi.Web.Services;
using caedisi.Web.DataModel;
using System.ServiceModel.DomainServices.Client;
using System.Linq;

namespace caedisi.Controller
{
    public class RecordStateController
    {
        private static object syncRoot = new Object();

        private RecordStateDomainServices _context;
        /// <summary>
        /// Gets the context.
        /// </summary>
        public RecordStateDomainServices Context
        {
            get { return _context; }
        }
       
        private static volatile RecordStateController instance;
        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static RecordStateController Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new RecordStateController();
                    }
                }
                return instance;
            }
        }
               

        /// <summary>
        /// Initializes a new instance of the <see cref="CellularAutomataController"/> class.
        /// </summary>
        public RecordStateController()
        {
            _context = new RecordStateDomainServices();
        }

        private RecordState _CurrentEntity;
        /// <summary>
        /// Gets or sets the current entity.
        /// </summary>
        /// <value>
        /// The current entity.
        /// </value>
        public RecordState CurrentEntity
        {
            get
            {
                return _CurrentEntity;
            }
            set
            {
                if (value == null)
                    _context.RecordStates.Detach(_CurrentEntity);
                else
                {
                    ClearAllLoadedObjects();
                    _context.RecordStates.Add(value);
                }
                _CurrentEntity = value;
            }
        }

        #region Record State
        /// <summary>
        /// Loads the specified cellular automata id.
        /// </summary>
        /// <param name="cellularAutomataId">The cellular automata id.</param>
        /// <param name="LoadCompleted">The load completed.</param>
        public void Load(int RecordStateId, Action<RecordState> LoadCompleted)
        {
            ClearAllLoadedObjects();

            EntityQuery<RecordState> query = _context.GetRecordStatesQuery().Where(RS => RS.RecordStateId == RecordStateId);

            //clearing previous results stored inside the domain context
            _context.Load(
                query,
                loadOperation =>
                {
                    if (!loadOperation.HasError
                        && _context.RecordStates != null
                        && _context.RecordStates.Count > 0)
                    {
                        _CurrentEntity = _context.RecordStates.First();  //_dataEntryContext.LocalUnions should always return a single entity                         
                    }


                    LoadCompleted(_CurrentEntity);
                },
                null
            );
        }


        /// <summary>
        /// Lazies the load all cellular automata.
        /// </summary>
        /// <param name="LoadCompleted">The load completed.</param>
        public void LazyLoadAllRecordState(Action<EntitySet<RecordState>> LoadCompleted)
        {
            EntityQuery<RecordState> query = _context.GetRecordStatesQuery();

            _context.Load(
               query,
               loadOperation =>
               {                  
                   if (loadOperation.HasError)
                   {
                       ;//TODO
                   }
                   LoadCompleted(_context.RecordStates);
               },
               null);
        }


        /// <summary>
        /// Removes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Remove(RecordState entity)
        {
            throw new NotImplementedException();
        }
        
        #endregion

        /// <summary>
        /// Saves the specified save completed.
        /// </summary>
        /// <param name="SaveCompleted">The save completed.</param>
        public void Save(Action<bool, Exception> SaveCompleted)
        {
            _context.SubmitChanges(
                  submitOperation =>
                  {
                      if (submitOperation.HasError)
                      {
                          ;
                      }
                      SaveCompleted(!submitOperation.HasError, submitOperation.Error);
                  },
                  null);
        }

        /// <summary>
        /// Gets a value indicating whether this instance has changes.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has changes; otherwise, <c>false</c>.
        /// </value>
        public bool HasChanges
        {
            get { return (_context != null ? _context.HasChanges : false); }
        }

        /// <summary>
        /// Rejects the changes.
        /// </summary>
        public void RejectChanges()
        {
            this._context.RejectChanges();
        }

        #region private methods

        /// <summary>
        /// Clears all loaded objects.
        /// </summary>
        private void ClearAllLoadedObjects()
        {
            _context.RecordStates.Clear();
            _CurrentEntity = null;
        }

        #endregion

    }
}
